<?php

/**
 * Description of upload
 *
 * @author YeFng
 */
namespace c\file;

class upload extends \control
{
    var $class = array('F'=>'\files');
    
    function ing($u_file, $dir='')
    {
        set_time_limit(90);
        
        // type
        $fileName = $this->F->charset_sys(str_replace('/', '', str_replace('\\', '', $u_file['name'])));
        if (\YF_f::$config['file_uploadType'] && !$this->F->has_ext($fileName, \YF_f::$config['file_uploadType']) )
            return \lang::g('not allowed file');
        
        //path
        $fileName = $this->buildFile($dir, $fileName);
        $file = FILE_DIR . $dir . $fileName;
        
        if ( !move_uploaded_file($u_file['tmp_name'], $file) )
            return \lang::g('Over');
		
        $r = array('status'=>TRUE, 
                    'path'=> $this->fileToPath($file),
                    'name'=>  $this->F->charset_p($fileName),
                    'size'=> filesize($file));
		return $r;
    }
    // 路径转换
    protected function fileToPath($file)
    {
    	return str_replace('\\', '/',$this->F->charset_p(substr($file, strlen(FILE_DIR))));
    }
    
    function buildFile($dir, $fileName)
    {
       $dir = FILE_DIR . $dir;
       $this->F->c_dir($dir);
       $fileName = (\YF_f::$arr['login_user'] ? \YF_f::$login_user_id : '-'.rand(0, 99)) .'_' . $fileName;
       $fileName = $this->F->i_filename($dir, $fileName);
       
       $f = $dir . $fileName;
       $this->F->w_file($f);
       chmod($f, 0777);
       
       return $fileName;
    }	
}
